# -*- coding: utf-8 -*-

import sqlite3
from Data.models import Book, BookInfo, engine
from Data.databaseApi import API


db = API(engine)
             
class Sqlite3Pipeline(object):

    def process_item(self, item, spider):
        print('=' * 20, '开始保存')
        # 查询书籍是否存在
        krags = {
            'name' : item['name'],
            'author' : item['author']
        }

        res = db.query(Book, krags)
        
        if len(res) <= 0:
            data = {
                'name': item['name'],
                'author': item['author'],
                'showUrl': item['showUrl'],
                'describe': item['describe'],
                'booktype' : item['booktype'],
                'downUrl': item['downUrl'],
                'wordNum': item['wordNum'],
                'state': item['state']
            }

            res, e = db.addone(Book, data)
            print('=' * 20, e)
            return e
        print('书籍已存在')
        return item

